#ifndef __LCD_H
#define __LCD_H		
#include "Arduino.h"
#include "stdlib.h"

//IO定义,启用直接操作寄存器后此处定义失效
  #define LCD_CS   0  // Do not define, chip select control pin permanently connected to 0V
  #define LCD_RS   1    // Data Command control pin
  #define LCD_WR   2
  #define LCD_RD   3  // Do not define, read pin permanently connected to 3V3
  #define LCD_RST  13    // Reset pin
  #define LCD_BL   16

  #define LCD_D0    4
  #define LCD_D1    5
  #define LCD_D2    6
  #define LCD_D3    7
  #define LCD_D4    8
  #define LCD_D5    10
  #define LCD_D6    11
  #define LCD_D7    12

/* 是否启用直接操作寄存器

取消注释则启用，可有效提高LCD操作速度，
但代码不再具有可移植性，
需要手动修改寄存器操作部分
*/
#define DIRECT_OPERATION_REGISTER

//IO操作
#ifdef DIRECT_OPERATION_REGISTER
	//直接操作寄存器，按照具体GPIO配置
	//GPIO置位（拉高）
	#define	LCD_CS_SET  GPIO.out_w1ts = (1 << LCD_CS)
	#define	LCD_RS_SET	GPIO.out_w1ts = (1 << LCD_RS)
	#define LCD_WR_SET  GPIO.out_w1ts = (1 << LCD_WR)
	#define LCD_RD_SET  GPIO.out_w1ts = (1 << LCD_RD)
	#define	LCD_RST_SET	GPIO.out_w1ts = (1 << LCD_RST)
	#define	LCD_BL_SET	GPIO.out_w1ts = (1 << LCD_BL)
	//GPIO复位（拉低）			
	#define	LCD_CS_CLR  GPIO.out_w1tc = (1 << LCD_CS)
	#define	LCD_RS_CLR	GPIO.out_w1tc = (1 << LCD_RS)
	#define LCD_WR_CLR  GPIO.out_w1tc = (1 << LCD_WR)
	#define LCD_RD_CLR  GPIO.out_w1tc = (1 << LCD_RD)
	#define	LCD_RST_CLR	GPIO.out_w1tc = (1 << LCD_RST)
	#define	LCD_BL_CLR	GPIO.out_w1ts = (1 << LCD_BL)
#else 
	//GPIO置位（拉高）
	#define	LCD_CS_SET  digitalWrite(LCD_CS,HIGH)
	#define	LCD_RS_SET	digitalWrite(LCD_RS,HIGH)
	#define LCD_WR_SET  digitalWrite(LCD_WR,HIGH)
	#define LCD_RD_SET  digitalWrite(LCD_RD,HIGH)
	#define	LCD_RST_SET	digitalWrite(LCD_RST,HIGH)
	#define	LCD_BL_SET	digitalWrite(LCD_BL,HIGH)
	//GPIO复位（拉低）			
	#define	LCD_CS_CLR  digitalWrite(LCD_CS,LOW)
	#define	LCD_RS_CLR	digitalWrite(LCD_RS,LOW)
	#define LCD_WR_CLR  digitalWrite(LCD_WR,LOW)
	#define LCD_RD_CLR  digitalWrite(LCD_RD,LOW)
	#define	LCD_RST_CLR	digitalWrite(LCD_RST,LOW)
	#define	LCD_BL_CLR	digitalWrite(LCD_BL,LOW)
#endif




/////////////////////////////////////用户配置区///////////////////////////////////	 
#define USE_HORIZONTAL  3 //定义液晶屏顺时针旋转方向 	0-0度旋转，1-90度旋转，2-180度旋转，3-270度旋转
#define BGR 0 // RGB色序 1-BGR 0-RGB 
//////////////////////////////////////////////////////////////////////////////////	  
//定义LCD的尺寸
#define LCD_W 128
#define LCD_H 160

//LCD重要参数集
typedef struct  
{										    
	uint16_t width;			//LCD 宽度
	uint16_t height;		//LCD 高度
	uint16_t id;		    //LCD ID
	uint8_t  dir;			//横屏还是竖屏控制：0，竖屏；1，横屏。	
	uint16_t wramcmd;		//开始写gram指令
	uint16_t setxcmd;		//设置x坐标指令
	uint16_t setycmd;		//设置y坐标指令	
	uint8_t  xoffset;    
	uint8_t	 yoffset;
}_lcd_dev; 	


//LCD参数
extern _lcd_dev lcddev;	//管理LCD重要参数

//TFTLCD部分外要调用的函数		   
extern uint16_t  POINT_COLOR;//默认红色    
extern uint16_t  BACK_COLOR; //背景颜色.默认为白色

//画笔颜色
#define WHITE       0xFFFF
#define BLACK      	0x0000	  
#define BLUE       	0x001F  
#define BRED        0XF81F
#define GRED 		0XFFE0
#define GBLUE		0X07FF
#define RED         0xF800
#define MAGENTA     0xF81F
#define GREEN       0x07E0
#define CYAN        0x7FFF
#define YELLOW      0xFFE0
#define BROWN 	    0XBC40 //棕色
#define BRRED 	    0XFC07 //棕红色
#define GRAY  		0X8430 //灰色
#define GRAY0       0xEF7D 
#define GRAY1       0x8410      	//灰色1      00000 000000 00000
#define GRAY2       0x4208 
//GUI颜色

#define DARKBLUE    0X01CF	//深蓝色
#define LIGHTBLUE   0X7D7C	//浅蓝色  
#define GRAYBLUE    0X5458 //灰蓝色
//以上三色为PANEL的颜色 
 
#define LIGHTGREEN  0X841F //浅绿色
#define LIGHTGRAY   0XEF5B //浅灰色(PANNEL)
#define LGRAY 		0XC618 //浅灰色(PANNEL),窗体背景色

#define LGRAYBLUE   0XA651 //浅灰蓝色(中间层颜色)
#define LBBLUE      0X2B12 //浅棕蓝色(选择条目的反色)
	    															  
void LCD_Init(void);
void LCD_DisplayOn(void);
void LCD_DisplayOff(void);
void LCD_Clear(uint16_t Color);	 
void LCD_SetCursor(uint16_t Xpos, uint16_t Ypos);
void LCD_DrawPoint(uint16_t x,uint16_t y);//画点
uint16_t  LCD_ReadPoint(uint16_t x,uint16_t y); //读点   
void LCD_SetWindows(uint16_t xStar, uint16_t yStar,uint16_t xEnd,uint16_t yEnd);

uint16_t LCD_RD_DATA(void);//读取LCD数据									    
void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue);
void LCD_WR_DATA(uint8_t data);
uint16_t LCD_ReadReg(uint8_t LCD_Reg);
void LCD_WriteRAM_Prepare(void);   
void LCD_SetParam(void);
void Lcd_WriteData_16Bit(uint16_t Data);
void LCD_direction(uint8_t direction );	
// void LCD_ShowChar(uint16_t x,uint16_t y,uint16_t fc, uint16_t bc, uint8_t num,uint8_t size,uint8_t mode);
// void LCD_ShowString(uint16_t x, uint16_t y, uint16_t fc, uint16_t bc,const uint8_t *str,uint8_t size,uint8_t mode);		  		 
#endif  
	 
	 



